package com.bdqn.elasticsearch1.controller;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Highlight;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.Suggester;
import com.bdqn.elasticsearch1.entity.Student;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class StudentController {
    @Autowired
    ElasticsearchClient client;

    @GetMapping("/auto")
    //路由到添加新书页面
    public String addStudent() {
        return "auto";
    }

    @GetMapping("/autocomplete")
    @ResponseBody
    public String autoComplete(String prefix) throws IOException {
        System.out.println("prefix = " + prefix);
        List<Student> list = new ArrayList<>();
        Suggester suggester = Suggester.of(s -> s.suggesters("university_suggest", p -> p.prefix(prefix).completion(c -> c.field("university").size(3).skipDuplicates(true))));
        SearchRequest searchRequest = new SearchRequest.Builder().index("student").suggest(suggester).build();
        SearchResponse<Student> search = client.search(searchRequest, Student.class);
        int size = search.suggest().get("university_suggest").get(0).completion().options().size();
        for (int i = 0; i < size; i++) {
            Student student = search.suggest().get("university_suggest").get(0).completion().options().get(i).source();
            list.add(student);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(list);
    }

    @GetMapping("/searchHighLight")
    public String searchHighLight() {
        return "searchHighLight";
    }

    @PostMapping("/searchHighLight")
    public ModelAndView search(String keyword) throws IOException {
        System.out.println("keyword = " + keyword);
        List<Student> list = new ArrayList<>();
        Query queryByName = Query.of(q -> q.match(m -> m.field("name").query(keyword)));
        String[] fields = {"name"};
        Highlight highlightBuilder = Highlight.of(h -> {
            for (String field : fields) {
                h.fields(field, h1 -> h1.preTags("<b><font color='red'>").postTags("</font></b>"));
            }
            return h;
        });
        SearchRequest searchRequest = new SearchRequest.Builder().index("student").query(queryByName).highlight(highlightBuilder).build();
        SearchResponse<Student> search = client.search(searchRequest, Student.class);
        for (Hit<Student> hit : search.hits().hits()) {
            Student student = hit.source();
            String name = hit.highlight().get("name").get(0);
            student.setName(name);
            list.add(student);
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("students", list);
        mv.setViewName("students");
        return mv;
    }
}